package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.WordOfRecycycleAdapter;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.WordOfMouthBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 口碑旺铺
 */
public class WordofMouthActivity extends BasePersonActivity {

    @BindView(R.id.yrecycleview_activity_wordfo_mouth)
    YRecycleview mYrecycleviewActivityWordfoMouth;
    private LoadingPagerHead mLoadingPagerHead;
    List<WordOfMouthBean> mWordOfMouthBeans=new ArrayList<>();
    private WordOfRecycycleAdapter mWordOfRecycycleAdapter;
    private int currentPosition=1;

    @Override
    protected View getContentView(Context context) {

        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(WordofMouthActivity.this, R.layout.activity_wordof_mouth);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };


       // setContentView(R.layout.activity_wordof_mouth);

        return mLoadingPagerHead;
    }

    /**
     * 重新加载
     */
    private void retry() {
        Observable<StatusCode<List<WordOfMouthBean>>> wordOfMothListObservable = ApiUtils.getApi().getWordOfMothList(Contans.defaultToken, 1, Contans.rows);
        HttpUtil.getInstance().toSubscribe(wordOfMothListObservable, new ProgressSubscriber<List<WordOfMouthBean>>(this) {
            @Override
            protected void _onNext(StatusCode<List<WordOfMouthBean>> listStatusCode) {
                List<WordOfMouthBean> data = listStatusCode.getData();
                if (listStatusCode.isFlag()&&data!=null&&data.size()>0){
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    mWordOfMouthBeans.clear();
                    mWordOfMouthBeans.addAll(data);
                    mWordOfRecycycleAdapter.notifyDataSetChanged();

                }else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                    ToastUtils.makeText(listStatusCode.getDetailMsg());
                }


            }

            @Override
            protected void _onError(String message) {

                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                ToastUtils.makeText(message);


            }
        },"",lifecycleSubject,false,true);


    }

    @Override
    protected void initData() {
        super.initData();
        mRlHeadRoot.setBackgroundColor(UIUtils.getColor(R.color.c_29CE7E));
        mTvTitle.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
        mTvTitle.setText("口碑旺铺");
        mIvBack.setImageResource(R.mipmap.fh_w);

        mWordOfRecycycleAdapter = new WordOfRecycycleAdapter(this,mWordOfMouthBeans);
        mYrecycleviewActivityWordfoMouth.setLayoutManager(new LinearLayoutManager(this));
        mYrecycleviewActivityWordfoMouth.setItemAnimator(new DefaultItemAnimator());
        mYrecycleviewActivityWordfoMouth.setAdapter(mWordOfRecycycleAdapter);
        retry();
        mYrecycleviewActivityWordfoMouth.setRefreshAndLoadMoreListener(new YRecycleview.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {

                Observable<StatusCode<List<WordOfMouthBean>>> wordOfMothListObservable = ApiUtils.getApi().getWordOfMothList(Contans.defaultToken, 1, Contans.rows);
                HttpUtil.getInstance().toSubscribe(wordOfMothListObservable, new ProgressSubscriber<List<WordOfMouthBean>>(WordofMouthActivity.this) {
                    @Override
                    protected void _onNext(StatusCode<List<WordOfMouthBean>> listStatusCode) {
                        mYrecycleviewActivityWordfoMouth.setReFreshComplete();
                        List<WordOfMouthBean> data = listStatusCode.getData();
                        if (listStatusCode.isFlag()&&data!=null&&data.size()>0){
                            mWordOfMouthBeans.clear();
                            mWordOfMouthBeans.addAll(data);
                            mWordOfRecycycleAdapter.notifyDataSetChanged();
                            currentPosition=1;
                            ToastUtils.makeText("刷新成功");

                        }else {
                            ToastUtils.makeText("刷新失败");
                        }

                    }

                    @Override
                    protected void _onError(String message) {
                        mYrecycleviewActivityWordfoMouth.setReFreshComplete();
                        ToastUtils.makeText(message);



                    }
                },"",lifecycleSubject,false,true);



            }

            @Override
            public void onLoadMore() {

                Observable<StatusCode<List<WordOfMouthBean>>> wordOfMothListObservable = ApiUtils.getApi().getWordOfMothList(Contans.defaultToken, currentPosition+1, Contans.rows);
                HttpUtil.getInstance().toSubscribe(wordOfMothListObservable, new ProgressSubscriber<List<WordOfMouthBean>>(WordofMouthActivity.this) {
                    @Override
                    protected void _onNext(StatusCode<List<WordOfMouthBean>> listStatusCode) {
                        mYrecycleviewActivityWordfoMouth.setloadMoreComplete();
                        List<WordOfMouthBean> data = listStatusCode.getData();
                        if (listStatusCode.isFlag()&&data!=null&&data.size()>0){
                            mWordOfMouthBeans.addAll(data);
                            currentPosition+=1;
                            mWordOfRecycycleAdapter.notifyDataSetChanged();
                        }else {
                            ToastUtils.makeText("没有更多数据了");
                        }

                    }

                    @Override
                    protected void _onError(String message) {
                        mYrecycleviewActivityWordfoMouth.setloadMoreComplete();
                        ToastUtils.makeText(message);


                    }
                },"",lifecycleSubject,false,true);





            }
        });

    }

    public static void startActivity(Context context){
        Intent intent=new Intent(context,WordofMouthActivity.class);
        context.startActivity(intent);


    }
}
